<%@page import="java.awt.color.ICC_ColorSpace"%>
<%@page import="org.apache.commons.lang.StringUtils"%>
<%@page import="bsh.StringUtil"%>
<%@page import="org.swim.remote.entities.ICollaboration"%>
<%@page import="org.swim.remote.entities.IUser"%>
<%@page import="org.swim.remote.entities.IFeedback"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" import="org.swim.web.utils.*"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.lang.WordUtils"%>
<!DOCTYPE html>
<html lang="en">
<head>
<%@include file="include/header.jsp"%>

<%
	List myCols = (List) request.getAttribute("myCols");
	ICollaboration colSel = (ICollaboration) request
			.getAttribute("collaboration");
	IUser me = (IUser) request.getAttribute("owner");
%>
<style type="text/css">

/* 	Collaboration */
.collaboration-item {
	padding: 15px;
	padding-top: 10px;
	margin: 15px;
}

.collaboration-item .alert {
	padding-right: 14px;
}

.collaboration-item p {
	line-height: normal;
}

.baloon {
	width: 90%;
	word-wrap: break-word;
	font-size: 15px;
	padding: 3px;
}
</style>

<title><%=SwimConst.getTitle()%></title>
</head>

<%
	if (colSel.getStatus() == ICollaboration.Status.CONFIRMED) {
%>
<body
	onload="startRefreshChat('<%=SwimConst.Url.Servlet.COLLABORATION%>?a=<%=Action.VIEWLASTMESSAGE%>', <%=colSel.getId()%>)">
	<%
		} else {
	%>

<body>
	<%
		}
	%>
	<%@include file="include/navbar.jsp"%>
	<%@include file="include/error-handling.jsp"%>

	<div class="container">
		<div class="row">
			<div class="span8 ui-corner-all" style="background: #DDD">
				<div class="row">
					<%
						if (SwimUtils.isLogged(request)) {
					%>
					<div class="span2 small-text">
						<ul class="nav nav-pills nav-stacked" style="margin: 5px">
							<%
								ICollaboration col;
									for (int i = 0; i < myCols.size(); i++) {
										col = (ICollaboration) myCols.get(i);
										if (col.equals(colSel)) {
							%>
							<li class="active"><a href="#"><i
									class="icon-chevron-right pull-right"></i><%=StringUtils.abbreviate(col.getTitle(), 15)%></a></li>

							<%
								} else {
							%>
							<li><a
								href="<%=SwimConst.Url.Servlet.COLLABORATION%>?a=<%=Action.VIEWCOLLABORATION%>&id=<%=col.getId()%>"><i
									class="icon-chevron-right pull-right"></i><%=StringUtils.abbreviate(col.getTitle(), 15)%></a></li>
							<%
								}
									}
							%>
						</ul>
					</div>
					<div class="span6">
						<%
							} else {
						%>
						<div class="span8">
							<%
								}
							%>
							<div class="row-fluid">
								<div class="collaboration-item medium-text ui-corner-all">
									<div class="span12">
										<h4>
											<%=colSel.getTitle()%>
											<%
												if (colSel.getStatus().equals(ICollaboration.Status.COMPLETED)) {
											%>
											<span class="label label-info pull-right">Completed</span>
											<%
												} else if (colSel.getStatus().equals(
														ICollaboration.Status.CONFIRMED)) {
											%>
											<span class="label label-success pull-right">Accepted</span>
											<%
												} else if (colSel.getStatus()
														.equals(ICollaboration.Status.REJECTED)) {
											%>
											<span class="label label-important pull-right">Rejected</span>
											<%
												} else if (colSel.getStatus().equals(
														ICollaboration.Status.REQUESTED)) {
											%>
											<span class="label label-warning pull-right">Requested</span>
											<%
												}
											%>
											<br />

											<%
												boolean ownerFound = false;
												if (SwimUtils.isLogged(request)) {

													if (colSel.getIUserBySenderId() != null) {
														if (colSel.getIUserBySenderId().getStatus() == IUser.Status.REGISTERED
																&& !colSel.getIUserBySenderId().equals(me)
																&& colSel.getSenderVisibility().equals(
																		ICollaboration.Visibility.VISIBLE)) {
															ownerFound = true;
											%>
											<span class="medium-text">Request by <a
												href="<%=SwimConst.Url.Servlet.PROFILE%>?who=
									<%=colSel.getIUserBySenderId().getId()%>"><%=SwimUtils.getName(colSel.getIUserBySenderId())%></a></span><br />
											<%
												} else if (colSel.getIUserBySenderId().equals(me)) {
															ownerFound = true;
											%>
											<span class="medium-text">Your request to <a
												href="<%=SwimConst.Url.Servlet.PROFILE%>?a=<%=Action.VIEWPROFILE%>&who=<%=colSel.getUserByReceiverId().getId()%>"><%=SwimUtils.getName(colSel
								.getUserByReceiverId())%></a></span><br />
											<%
												} else if (colSel.getIUserBySenderId().getStatus() == IUser.Status.UNREGISTERED) {
															ownerFound = true;
											%>
											<span class="medium-text"><a
												href="mailto:<%=colSel.getIUserBySenderId().getEmail()%>"><%=colSel.getIUserBySenderId().getEmail()%></a></span><br />
											<%
												}
													}
												}
											%>


											<%
												if (!ownerFound) {
											%>
											<span class="medium-text">Anonymous request</span><br />
											<%
												}
											%>

										</h4>
										<!-- Start Action on collaboration status -->
										<%
											if (colSel.getUserByReceiverId() != null
													&& colSel.getUserByReceiverId().equals(me)
													&& colSel.getStatus() == ICollaboration.Status.REQUESTED) {
										%>
										<div class="btn-group">
											<a class="btn btn-success"
												href="<%=SwimConst.Url.Servlet.COLLABORATION%>?a=<%=Action.ACCEPTCOLLABORATION%>&collaborationId=<%=colSel.getId()%>">Accept</a>
											<a class="btn btn-danger"
												href="<%=SwimConst.Url.Servlet.COLLABORATION%>?a=<%=Action.REJECTCOLLABORATION%>&collaborationId=<%=colSel.getId()%>">Reject</a>
										</div>
										<%
											} else if (colSel.isMy(me)
													&& colSel.getStatus() == ICollaboration.Status.CONFIRMED) {
										%>
										<div class="btn-group">
											<form method="post"
												action="<%=SwimConst.Url.Servlet.COLLABORATION%>?a=<%=Action.COMPLETECOLLABORATION%>">
												<%=FormHelper.getHiddenInput("collaborationId",
						colSel.getId() + "")%>
												<button class="btn btn-info" type="submit">End
													collaboration</button>

											</form>

										</div>
										<%
											}
										%>
										<!-- End Action on collaboration status -->

									</div>
									<div class="span12">
										<!-- Start Feedback manager -->
										<%
											boolean isAlreadyVote = false;
											IFeedback feedback;
											for (int i = 0; i < colSel.getFeedbacksSet().size(); i++) {
												feedback = (IFeedback) colSel.getFeedbacksSet().toArray()[i];
												if (feedback.getIUserBySenderId() != null
														&& feedback.getIUserBySenderId().equals(me))
													isAlreadyVote = true;
										%>

										<div id="feedback" class="row form-inline">
											<div class="span12" style="padding-top: 6px;">
												<span class="small-text"> <%
 	if (feedback.getIUserBySenderId() != null
 				&& feedback.getIUserBySenderId().equals(me)) {
 %> About collaborator · <%
 	} else {
 %> About you · <%
 	}
 %>
												</span> <span class="badge badge-info"> <%
 	for (int j = 0; j < 5; j++) {
 			if (j < feedback.getValuation()) {
 %> <i class="icon-star"></i> <%
 	} else {
 %> <i class="icon-star-empty"></i> <%
 	}
 		}
 %> <span class="small-text" style="margin-left: 3px;"><%=feedback.getComment()%></span>
												</span>
											</div>
										</div>

										<%
											}

											if (SwimUtils.isLogged(request)
													&& colSel.getStatus() == ICollaboration.Status.COMPLETED
													&& colSel.isMy(me)
													&& !isAlreadyVote
													&& SwimUtils.getCollaborator(colSel, me) != null
													&& SwimUtils.getCollaborator(colSel, me).getStatus()
															.equals(IUser.Status.REGISTERED)) {
										%>
										<form id="feedback" class="row form-inline" method="post"
											action="<%=SwimConst.Url.Servlet.COLLABORATION%>?a=<%=Action.SENDFEEDBACK.name().toLowerCase()%>">
											<input type="hidden" name="valuation">
											<%=FormHelper.getHiddenInput("collaborationId",
						colSel.getId() + "")%>
											<div style="padding-top: 6px;">
												<span class="small-text">About collaborator · </span><span
													class="badge badge-info"><a
													href="javascript:setVote(1)"
													class="ratings_stars icon-star-empty"></a><a
													href="javascript:setVote(2)"
													class="ratings_stars icon-star-empty"></a><a
													href="javascript:setVote(3)"
													class="ratings_stars icon-star-empty"></a><a
													href="javascript:setVote(4)"
													class="ratings_stars icon-star-empty"></a><a
													href="javascript:setVote(5)"
													class="ratings_stars icon-star-empty"></a> </span>
											</div>
											<div id="comment" style="margin-top: 5px;">
												<span class="input-append"> <input name="comment"
													type="text" class="span12" placeholder="Type a comment...">
													<button class="btn" type="submit">Vote</button>
												</span>
											</div>
										</form>
										<%
											}
										%>

										<!-- End Feedback manager -->
									</div>

									<%
										if (SwimUtils.isLogged(request)
												&& colSel.getStatus() == ICollaboration.Status.CONFIRMED
												&& colSel.isMy(me)
												&& (colSel.getIUserBySenderId() == null || colSel
														.getIUserBySenderId().getStatus() == IUser.Status.REGISTERED)) {
									%>
									<!-- Start send comment -->
									<div class="row-fluid">
										<form id="sendMessageForm" action="javascript:postComment()"
											data-action="<%=SwimConst.Url.Servlet.COLLABORATION%>?a=<%=Action.SENDMESSAGE.name().toLowerCase()%> ">
											<div class="span10">
												<%=FormHelper.getHiddenInput("collaborationId",
						colSel.getId() + "")%>
												<textarea name="message" class="span" rows="3"
													style="resize: none; height: 70px"></textarea>
											</div>
											<div class="span2" style="height: 70px">
												<button class="btn" style="height: 100%" type="submit">Send</button>
											</div>
										</form>
									</div>
									<!-- End send comment -->
									<%
										}
									%>

									<div id="collaborationChat"
										class="span12 collaboration-message"
										style="height: 100%; overflow: auto; max-height: 350px; margin-top: 10px; margin-bottom: 20px">
										<%
											request.setAttribute("messages", colSel.getMessagesList());
										%>
										<%@include file="message-list-item.jsp"%>
									</div>

								</div>
							</div>
						</div>
					</div>
				</div>
				<%@include file="include/notifications.jsp"%>
			</div>
		</div>
		<%@include file="include/footer.jsp"%>
		<%@include file="include/script-common.jsp"%>
</body>
</html>
